package api

import (
	"dake-farmer-home/internal/models"
	"dake-farmer-home/internal/pkg/common"
	"dake-farmer-home/internal/services"
	"github.com/kataras/iris/v12"
)

type UserController struct {
	Ctx iris.Context
}

func (u *UserController) PostLogin() *common.JsonResult {
	user := &models.User{}
	err := u.Ctx.ReadJSON(user)
	if err != nil {
		return common.Error("参数有误")
	}
	_, err = services.UserService.Login(user.Account, user.Password)
	if err != nil {
		return common.Error(err.Error())
	}
	return common.Success("success")
}

func (u *UserController) PostRegister() *common.JsonResult {
	user := &models.User{}
	err := u.Ctx.ReadJSON(user)
	if err != nil {
		return common.Error("参数有误")
	}
	user, err = services.UserService.Register(user.Account, user.Password)
	if err != nil {
		return common.Error(err.Error())
	}
	return common.Success("成功")
}
